//获取应用实例
const app = getApp();
let request = require("../../utils/request.js");
import {
  auditedPark,
  getParkProfitApi,
  getParkOrderStatisticsApi,
} from "../../api/index";
Page({
  data: {
    barHeight: wx.getMenuButtonBoundingClientRect().bottom,
    pageHeight: app.globalData.pageHeight,
    paddingTop: app.globalData.paddingTop,
    changeParkingDialog: false,
    handrailDialog: false,
    parkInfo: {},
    parkingOwnerData: {},
    parkList: [],
    parkName: "",
    parkId: "",
    parkSource: "",
    aisleList: [],
    selectAisleData: {},
    todayIncomeFromShop: 0,
    managerId: "",
    dayOrderStatistics:{},
    parkProfitInfo:{},
    parkColumns:[],
  },
  onLoad: function () {
    let token = wx.getStorageSync("token");
    if (!token || token == "") {
      // 清除所有缓存
      wx.clearStorageSync();
      wx.redirectTo({ url: "/pages/login/login" });
      return;
    }
  },
  async onShow() {
    // 查询已审核通过车场
    let selectPark = this.selectComponent('#selectPark'); // 页面获取自定义组件实例
    selectPark.getAuditedPark(); // 通过实例调用组件事件
    let managerId = wx.getStorageSync("userId");
    let token = wx.getStorageSync("token");
    if (!token || token == "") {
      wx.redirectTo({ url: "/pages/login/login" });
      return;
    }
    let parkId = wx.getStorageSync("parkId");
    let parkColumns = wx.getStorageSync('parkColumns')
    this.setData({
      parkId: parkId ? parkId : "",
      managerId: managerId ? managerId : "",
      parkColumns:parkColumns?parkColumns:[]
    });
    // await this.getAuditedPark(managerId);
    if (parkId != null) {
      //  获取停车场主信息
      // this.getParkingOwnerData(this.data.parkId);
      this.getParkOrderStatistics()
      this.getParkProfit();
    }
  },


  /**
   * 获取停车场主信息
   */
  getParkingOwnerData(id) {
    request
      .http("/order/orderInfo/getParkingOwnerData", "GET", { parkId: id })
      .then((res) => {
        let data = res.data;
        if (data.code == 200) {
          this.setData({
            parkingOwnerData: data.data,
            parkName: data.data.parkName,
            parkId: data.data.parkId,
            parkSource: data.data.parkSource,
          });
          wx.setStorageSync("parkId", data.data.parkId);
          wx.setStorageSync("parkInfo", {
            avatar: data.data.avatar,
            mobilePhone: data.data.mobilePhone,
            nickname: data.data.nickname,
            realName: data.data.realName,
            parkId: data.data.parkId,
            isOwner: data.data.isOwner,
            parkSource: data.data.parkSource,
            reserveStatus: data.data.reserveStatus,
          });
        }
      });
  },

  /**
   * 更换停车场
   */
  changeParking() {
    if (!this.data.parkName) {
      getApp().showToast("您暂无停车场");
      return false;
    }
    this.setData({
      changeParkingDialog: true,
    });
  },
  /**
   * 关闭切换停车场弹窗
   */
  hiddenChangeParkingDialog() {
    this.setData({
      changeParkingDialog: false,
    });
  },

  // 选择车场
  bindChangeParkName(e) {
    const { parkId, parkName } = e.detail;
    this.setData({
      parkName,
      parkId,
    });
    wx.setStorageSync('parkId', parkId);
    wx.setStorageSync('parkName', parkName)
    this.getParkOrderStatistics()
    this.getParkProfit();
  },

  /**
   * 获取通道码
   */
  openHandrailDialog() {
    let that = this;
    request
      .http("/park/park-entrance/getParkChannels", "POST", {
        parkId: this.data.parkId,
        parkSource: this.data.parkSource,
      })
      .then((res) => {
        let data = res.data;
        if (data.code == 200) {
          if (data.data.status == 1) {
            that.setData({
              aisleList: data.data.channels,
              handrailDialog: true,
            });
          } else {
            wx.showToast({
              title: data.data.msg,
              icon: "none",
            });
          }
        }
      });
  },

  surveillance() {
    wx.showToast({
      title: "开发中",
      icon: "none",
    });
  },


  //   查询已审核的车场
  async getAuditedPark(managerId) {
    const { data: res } = await auditedPark({ managerId });
    // console.log(res.data);
    if (res.data.length > 0) {
      this.setData({
        parkList: res.data,
        parkColumns:res.data
      });
    }
  },

  //查询查询车场当日、当月盈利
  async getParkProfit() {
    const { data: res } = await getParkProfitApi({ parkId:this.data.parkId });
    // console.log(res);
    this.setData({
      parkProfitInfo: res.data,
    });
  },

  //每日订单统计
  async getParkOrderStatistics() {
    const {data:res} = await getParkOrderStatisticsApi({ parkId:this.data.parkId });
    this.setData({
        dayOrderStatistics:res.data
    })
  },

  //   跳转页面
  async toPage(e) {
    const { type } = e.currentTarget.dataset;
    if (type) {
      getApp().showToast("正在开发,敬请期待! ");
      return false;
    }
    const isParkId = getApp().isRegisterPark();
    if (isParkId) {
      const { url } = e.currentTarget.dataset;
      wx.navigateTo({
        url:url+'?parkId='+this.data.parkId,
      });
    }
  },
  // 跳转备案审批
  toPageFilingApproval(e) {
    const { url } = e.currentTarget.dataset;
    wx.navigateTo({ url  });
  },
    /**
   * 右上角分享功能
   */
  onShareAppMessage: function (res) {
    return {
      title: "",
      path: "/pages/index/index",
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      },
    };
  },
});
